package com.xiangban.service.impl;

import com.alibaba.fastjson.JSON;
import com.xiangban.dao.ShopMapper;
import com.xiangban.exception.BizException;
import com.xiangban.model.Shop;
import com.xiangban.model.ShopLogin;
import com.xiangban.model.dto.PermissionMenuDto;
import com.xiangban.model.res.ApiResult;
import com.xiangban.model.res.ApiResultCode;
import com.xiangban.service.ShopService;
import com.xiangban.utils.CommonUtils;
import com.xiangban.utils.RedisUtil;
import com.xiangban.utils.TreeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.xiangban.model.dto.ShopLoginDto;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

import static com.xiangban.utils.CommonUtils.BizEmpty;
import static com.xiangban.utils.RedisUtil.*;

/**
 * @Author Mr. MaYaYun
 * @Data 2021/11/16 16:35
 */
@Service
public class ShopServiceImpl implements ShopService {
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private CommonUtils commonUtils;
    @Autowired
    private HttpServletRequest request;


    public Object login(ShopLogin shopLogin) {
        HashMap<String, String> res = new HashMap<>();
        ShopLogin shopInfo = shopMapper.shopInfoByUserNameAndPhone(shopLogin);
        if (shopInfo==null){
            throw new BizException(ApiResultCode.FAILED_ACCOUNT);
        }
        if (CommonUtils.compare(shopInfo.getPassword(), CommonUtils.MD5(shopLogin.getPassword()))){

            res.put("token",generateToken(shopInfo));
        }else {
            throw new BizException(ApiResultCode.FAILED_LOGIN);
        }
        return res;

    }

    public String generateToken(ShopLogin shopLogin) {
        String newToken = CommonUtils.generateUUID();
        HashMap<String, Object> loginInfo = new HashMap<>();
        loginInfo.put("time", new Date().getTime());
        loginInfo.put("shopId", shopLogin.getShopId());
        loginInfo.put("pid", shopLogin.getPid());
        //获取上次token
        String oldToken = redisUtil.get(fex_shop + shopLogin.getShopId());
        //清除上次登录token
//        redisUtil.del(fex_shop+oldToken,fex_shop+shopLogin.getShopId());
//        redisUtil.set(fex_shop + shopLogin.getShopId(), newToken, tokenTime);
        redisUtil.set(fex_shop +newToken, JSON.toJSONString(loginInfo), tokenTime);
        return newToken;
    }

    public ShopLoginDto getShopPermissionInformation() {
        String shopId = commonUtils.getShopId(request);
        ShopLogin shopLogin = new ShopLogin();
        shopLogin.setShopId(Integer.parseInt(shopId));
        //根据商家id获取商家角色信息
        ShopLoginDto shopLoginDto = shopMapper.getShopInfo(shopLogin);
        List<Object> permissionMenu = getPermissionMenu(Integer.parseInt(shopId),"CM",commonUtils.getToken(request));
        shopLoginDto.setPermissionMenus(permissionMenu);
        ArrayList<String> strings = new ArrayList<>();
        strings.add(shopLoginDto.getType());
        shopLoginDto.setRoles(strings);
        return shopLoginDto;

    }
    //获取权限菜单
    public List<Object> getPermissionMenu(Integer shopId,String type,String token){
        if (redisUtil.hasKey(fex_shop_permission+token)){
            return (List<Object>) redisUtil.lGetIndex(fex_shop_permission+token,0);
        }else {
            PermissionMenuDto permissionMenu = new PermissionMenuDto();
            permissionMenu.setShopId(shopId);
            permissionMenu.setType(type);
            List<PermissionMenuDto> menuAndPermission = shopMapper.getMenuAndPermission(permissionMenu);
            List<Object> tree = Collections.singletonList(TreeUtil.tree(menuAndPermission));
            redisUtil.lSet(fex_shop_permission+token,tree,permissionTime);
        }
        return  (List<Object>) redisUtil.lGetIndex(fex_shop_permission+token,0);

    }

    //失效权限菜单
    public void losePermissionMenu(){
        String token = commonUtils.getToken(request);
        redisUtil.lLeftPop(fex_shop_permission+token);
    }
    public void loseToken(){
        String shopId = commonUtils.getShopId(request);
        String token = commonUtils.getToken(request);
        redisUtil.del(fex_shop+shopId);
        redisUtil.del(fex_shop+token);
    }

    public Object getShopInformation() {
        String shopId = commonUtils.getShopId(request);
        Shop shop = new Shop();
        shop.setShopId(Integer.parseInt(shopId));
        return shopMapper.getShopInformation(shop);
    }

    public void updateShopInformation(Shop shop) {
        String shopId = commonUtils.getShopId(request);
        shop.setShopId(Integer.parseInt(shopId));
        shopMapper.updateShopInformation(shop);
    }

    public ArrayList<String> getPermission(Integer shopId){
        ArrayList<String> permissions = new ArrayList<>();
        PermissionMenuDto permissionMenuDto = new PermissionMenuDto();
        permissionMenuDto.setShopId(shopId);
        permissionMenuDto.setType("B");
        List<PermissionMenuDto> menuAndPermission = shopMapper.getMenuAndPermission(permissionMenuDto);
        if (menuAndPermission.size()>0){
            permissions = shopMapper.getPermission(menuAndPermission);
            return permissions;
        }
        return permissions;
    }

    @Override
    public void updateShopPassword(ShopLogin shopLogin) {
        if (BizEmpty(shopLogin.getPassword())){
            throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
        }
        String shopId = commonUtils.getShopId(request);
        String password = CommonUtils.MD5(shopLogin.getPassword());
        shopLogin.setShopId(Integer.parseInt(shopId));
        shopLogin.setPassword(password);
        shopMapper.updateShopPassword(shopLogin);
    }
}
